package com.itheima.other;

import com.itheima.RabbitUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @作者 itcast
 * @创建日期 2020/11/4 11:17
 **/
public class MqProducer3 {
    Channel channel;
    private static final String RED_QUEUE = "red_queue";
    private static final String GREEN_QUEUE = "green_queue";
    private static final String YELLOW_QUEUE = "yellow_queue";
    private static final String COLOR_QUEUE = "color_queue";

    /**
     * 广播模式
     * @throws IOException
     */
    @Test
    public void topicTest() throws IOException {
        String exchangeName = "topic_exchange";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC);
        // 绑定交换机和队列关系
        channel.queueBind(RED_QUEUE,exchangeName,"#.red");
        channel.queueBind(GREEN_QUEUE,exchangeName,"green.*");
        channel.queueBind(YELLOW_QUEUE,exchangeName,"*.yellow.*");
        channel.queueBind(COLOR_QUEUE,exchangeName,"#.red.#");
        channel.basicPublish(exchangeName,"xxx.red.ccc",null,new String("主题模式的消息").getBytes());
    }

    /**
     * 路由模式
     * @throws IOException
     */
    @Test
    public void directTest() throws IOException {
        String exchangeName = "direct_exchange";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT);
        // 绑定交换机和队列关系
        channel.queueBind(RED_QUEUE,exchangeName,"red");
        channel.queueBind(GREEN_QUEUE,exchangeName,"green");
        channel.queueBind(YELLOW_QUEUE,exchangeName,"yellow");
        channel.queueBind(COLOR_QUEUE,exchangeName,"color");
        channel.basicPublish(exchangeName,"red",null,new String("测试").getBytes());
    }

    /**
     * 广播模式
     * @throws IOException
     */
    @Test
    public void fanoutTest() throws IOException {
        String exchangeName = "fanout_exchange";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT);
        // 绑定交换机和队列关系
        channel.queueBind(RED_QUEUE,exchangeName,"");
        channel.queueBind(GREEN_QUEUE,exchangeName,"");
        channel.queueBind(YELLOW_QUEUE,exchangeName,"");
        channel.queueBind(COLOR_QUEUE,exchangeName,"");
        channel.basicPublish(exchangeName,"",null,new String("广播模式的消息").getBytes());
    }


    @Test
    public void createQueues() throws Exception{
        // 创建4个颜色队列
        channel.queueDeclare(RED_QUEUE,true,false,false,null);
        channel.queueDeclare(GREEN_QUEUE,true,false,false,null);
        channel.queueDeclare(YELLOW_QUEUE,true,false,false,null);
        channel.queueDeclare(COLOR_QUEUE,true,false,false,null);
    }
    @Before
    public void init(){
        channel = RabbitUtils.getChannel();
    }
    @After
    public void close(){
        RabbitUtils.destory();
    }
}
